import Vue from 'vue'
import VueRouter from 'vue-router'
import UserDashboard from '@/views/Dashboard.vue'
import Learn from '@/views/Learn.vue'
import Review from '@/views/Review.vue'
import Report from '@/views/Report.vue'
import Community from '@/views/Community.vue'
import Profile from '@/views/Profile.vue'
import SmartRecommendInterface from '@/components/SmartRecommendInterface.vue'
import Practice from '@/views/Practice.vue'
import Task from '@/views/Task.vue'
import StudyPlan from '@/views/StudyPlan.vue'
import KnowledgeBase from '@/views/KnowledgeBase.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/dashboard',
    component: UserDashboard,
    meta: { requiresAuth: false } // 首页不需要登录
  },
  {
    path: '/learn',
    component: Learn,
    meta: { requiresAuth: true }, // 学习页面需要登录
    children: [
      {
        path: '',
        name: 'learn-category',
        component: () => import('@/components/CategorySelection.vue')
      },
      {
        path: 'mode/:categoryId/:subCategoryId',
        name: 'learn-mode',
        component: () => import('@/components/ModeSelection.vue'),
        props: true
      },
      {
        path: 'knowledge/:categoryId/:subCategoryId',
        name: 'learn-knowledge',
        component: () => import('@/components/KnowledgeLearning.vue'),
        props: true
      },
      {
        path: 'question-mode/:categoryId/:subCategoryId',
        name: 'learn-question-mode',
        component: () => import('@/components/QuestionModeSelection.vue'),
        props: true
      },
      {
        path: 'practice/:categoryId/:subCategoryId/:studyModeId/:questionTypeId',
        name: 'learn-practice',
        component: () => import('@/components/QuestionInterface.vue'),
        props: true
      }
    ]
  },
  {
    path: '/practice',
    component: Practice,
    meta: { requiresAuth: true } // 练习页面需要登录
  },
  {
    path: '/task',
    component: Task,
    meta: { requiresAuth: true } // 学习任务页面需要登录
  },
  {
    path: '/smart-recommend',
    component: SmartRecommendInterface,
    meta: { requiresAuth: true } // 智能推荐需要登录
  },
  {
    path: '/review',
    component: Review,
    meta: { requiresAuth: true } // 复习页面需要登录
  },
  {
    path: '/report',
    component: Report,
    meta: { requiresAuth: true } // 数据报告需要登录
  },
  {
    path: '/study-plan',
    component: StudyPlan,
    meta: { requiresAuth: true } // 学习方案需要登录
  },
  {
    path: '/knowledge-base',
    component: KnowledgeBase,
    meta: { requiresAuth: false } // 知识库不需要登录
  },
  {
    path: '/community',
    component: Community,
    meta: { requiresAuth: false } // 社区页面不需要登录
  },
  {
    path: '/profile',
    component: Profile,
    meta: { requiresAuth: true } // 个人中心需要登录
  },
  {
    path: '/achievements',
    component: Profile,
    meta: { requiresAuth: true } // 成就勋章需要登录
  },
  {
    path: '/settings',
    component: Profile,
    meta: { requiresAuth: true } // 设置页面需要登录
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫：检查用户登录状态
router.beforeEach((to, from, next) => {
  // 检查路由是否需要登录
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  
  // 获取用户登录状态
  const user = JSON.parse(localStorage.getItem('user')) || null
  const isLoggedIn = !!(user && (user.username || user.name))
  
  if (requiresAuth && !isLoggedIn) {
    // 需要登录但用户未登录，重定向到首页并显示登录提示
    next({
      path: '/dashboard',
      query: { 
        redirect: to.fullPath,
        showLogin: 'true'
      }
    })
  } else {
    // 不需要登录或已登录，正常访问
    next()
  }
})

// 全局处理重复导航错误
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      throw err
    }
  })
}

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      throw err
    }
  })
}

export default router
